package com.alibaba.citrus.ots.platform.enhance.facade.service;

import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcots.model.dto.EmployeeGetCurrentLoginResponse;
import com.epoch.app.bcots.service.BcOtsService;
import com.epoch.app.otsplatformenhance.goodsfavorites.dto.UpdateResponse;
import com.epoch.app.otsplatformenhance.goodsfavorites.model.GoodsFavorites;
import com.epoch.app.otsplatformenhance.goodsfavorites.service.GoodsFavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@Primary
public class GoodsFavoritesServiceImpl implements GoodsFavoritesService {

    @Autowired
    private BcOtsService bcOtsService;

    @Override
    public Result<Boolean> updateOrInsertFavorites(GoodsFavorites goodsFavorites){
        if (Objects.isNull(goodsFavorites)) {
            return Result.fail("OTS-01-005-02-15-015","更新或新增收藏夹不能为空!");
        }
        EmployeeGetCurrentLoginResponse employee = bcOtsService.employeeGetCurrentLogin();

        if (Objects.isNull(employee)||Objects.isNull(employee.getEmployeeId())){
            return Result.fail("OTS-01-005-02-15-017","货品收藏夹更新或新增,获取当前登陆人信息错误!");
        }

        goodsFavorites.setUserId(employee.getEmployeeId());
        goodsFavorites.setUpdaterId(employee.getEmployeeId());

        if (Objects.isNull(goodsFavorites.getId())) {
            //insert operate
            goodsFavorites.setCreatorId(employee.getEmployeeId());
            create(goodsFavorites);
            return Result.success(Boolean.TRUE);
        }

        //or update
        goodsFavorites.setCreatorId(employee.getEmployeeId());
        UpdateResponse update = update(goodsFavorites);
        if (Objects.nonNull(update)&&Objects.nonNull(update.getCount())&&update.getCount()>0){
            return Result.success(Boolean.TRUE);
        }
        return Result.fail("OTS-01-005-02-15-016","数据更新失败!");
    }
}
